function get_host_arch(){
  local IS_Linux=$(uname -a | grep "Linux")
  local IS_X86_64=$(uname -a | grep "X86_64")
  user=`whoami`
  if [ "$IS_Linux" ]; then
    HOST_ARCH=linux
    PREFIX="/home/${user}"
  elif [ "$IS_X86_64" ]; then
    HOST_ARCH=mac-x64
    PREFIX="/Users/${user}"
  else
    HOST_ARCH=mac-arm64
    PREFIX="/Users/${user}"
  fi
}

function upload_config(){
  if [ $HOST_ARCH == "mac-arm64" ] && [ $VERSION_NAME == "3.22" ];then
    if [ ! -d src/out/host_release_arm64 ];then
      echo ${BRANCH_DIR}/src/out/host_release_arm64 not exist
      exit 1
    fi
    cp $SCRIPT_DIR/arch/$HOST_ARCH/flutter_tester/3.22/mac-arm64/flutter_tester src/out/host_release_arm64/
  fi
  if [ ! -d src/out/host_release ];then
    echo ${BRANCH_DIR}/src/out/host_release not exist
    exit 1
  fi
  cp $SCRIPT_DIR/arch/$HOST_ARCH/upload_template.json ./upload_template.json
  cp $SCRIPT_DIR/arch/$HOST_ARCH/flutter_tester/${VERSION_NAME}/${HOST_ARCH}/flutter_tester src/out/host_release/
}

function upload_branch(){
  if [ $HOST_ARCH == "" ] || [ $VERSION_NAME == "" ];then
    echo "error: host arch or version name is null!"
    exit 1
  fi
  echo "upload arch " $HOST_ARCH " upload version "$VERSION_NAME
  echo "current dir is " ${BRANCH_DIR}
  upload_config
  #return

  if [ $HOST_ARCH == "linux" ];then
    python3 src/flutter/attachment/scripts/zip_artifacts.py &&
    python3 src/flutter/attachment/scripts/auto_compile.py &&
    python3 src/flutter/attachment/scripts/zip_dart_sdk.py &&
    python3 src/flutter/attachment/scripts/upload_dart_sdk.py &&
    python3 src/flutter/attachment/scripts/zip_sky_engine.py &&
    python3 src/flutter/attachment/scripts/upload_sky_engine.py &&
    python3 src/flutter/attachment/scripts/zip_flutter_patched_sdk.py &&
    python3 src/flutter/attachment/scripts/upload_flutter_patched_sdk.py
  elif [ $HOST_ARCH == "mac-arm64" ] && [ $VERSION_NAME == "3.7" ];then
    python3 src/flutter/attachment/scripts/zip_artifacts.py &&
    python3 src/flutter/attachment/scripts/auto_compile.py &&
    python3 src/flutter/attachment/scripts/zip_dart_sdk.py --arch arm64 &&
    python3 src/flutter/attachment/scripts/upload_dart_sdk.py --arch arm64
  elif [ $HOST_ARCH == "mac-arm64" ] && [ $VERSION_NAME == "3.27" ];then
    python3 src/flutter/attachment/scripts/zip_artifacts.py &&
    python3 src/flutter/attachment/scripts/auto_compile.py &&
    python3 src/flutter/attachment/scripts/zip_dart_sdk.py --arch arm64 &&
    python3 src/flutter/attachment/scripts/upload_dart_sdk.py --arch arm64
  elif [ $HOST_ARCH == "mac-arm64" ] && [ $VERSION_NAME == "3.22" ];then
    git -C src/flutter stash &&
    git -C src/flutter apply $SCRIPT_DIR/arch/$HOST_ARCH/patch/upload_arm64.diff  &&
    python3 src/flutter/attachment/scripts/zip_artifacts.py &&
    python3 src/flutter/attachment/scripts/auto_compile.py &&
    python3 src/flutter/attachment/scripts/zip_dart_sdk.py &&
    python3 src/flutter/attachment/scripts/upload_dart_sdk.py &&
    git -C src/flutter stash &&
    git -C src/flutter apply $SCRIPT_DIR/arch/$HOST_ARCH/patch/upload_x64.diff  &&
    cp $SCRIPT_DIR/arch/$HOST_ARCH/upload_template.json.x64 ./upload_template.json &&
    cp $SCRIPT_DIR/arch/$HOST_ARCH/flutter_tester/${VERSION_NAME}/mac-x64/flutter_tester src/out/host_release/ &&
    python3 src/flutter/attachment/scripts/zip_artifacts.py &&
    python3 src/flutter/attachment/scripts/auto_compile.py &&
    python3 src/flutter/attachment/scripts/zip_dart_sdk.py &&
    python3 src/flutter/attachment/scripts/upload_dart_sdk.py
    if [ $? != 0 ];then
      exit 1
    fi
  else
    python3 src/flutter/attachment/scripts/zip_artifacts.py &&
    python3 src/flutter/attachment/scripts/auto_compile.py &&
    python3 src/flutter/attachment/scripts/zip_dart_sdk.py &&
    python3 src/flutter/attachment/scripts/upload_dart_sdk.py
  fi
}

function list_branch(){
  VERSION_NAME=`echo $VERSION_CONFIG | jq -r ".[0].version_name"`
  if [[ "${BUILD_FLUTTER_VERSION[@]}"  =~ "${VERSION_NAME}" ]]; then
    BRANCHES=`echo $VERSION_CONFIG | jq -r ".[0].branches"`
    BRANCH_NUM=`echo $BRANCHES | jq -r ".[0].branch_number"`
    for ((BRANCH_INDEX=1; BRANCH_INDEX<=BRANCH_NUM; BRANCH_INDEX++))
    do
      BRANCH_DATA=`echo $BRANCHES | jq -r ".[0].branch${BRANCH_INDEX}"`
      BRANCH_DIR=`echo $BRANCH_DATA | jq -r '.[0].branch_dir'`
      BRANCH_DIR=${PREFIX}/${BRANCH_DIR}
      echo branch dir is $BRANCH_DIR
      if [ ! -d "${BRANCH_DIR}" ] || [ ! -d "${BRANCH_DIR}/src/flutter" ]; then
        echo ${BRANCH_DIR}/src/flutter not exist!
        exit 1
      fi
      BRANCH_API=`echo $BRANCH_DATA | jq -r '.[0].api_version'`
      if [[ "${UPLOAD_API[@]}"  =~ "${BRANCH_API}" ]]; then
        cd ${BRANCH_DIR}
        upload_branch
      fi
    done
  fi
}

function list_flutter_version(){
  FLUTTER_VERSIONS=`cat $CONFIG_JSON | jq -r '.flutter_versions'`
  FLUTTER_VERSION_NUM=`echo $FLUTTER_VERSIONS | jq -r '.[0].flutter_version_num'`
  
  for ((INDEX=1; INDEX<=FLUTTER_VERSION_NUM; INDEX++))
  do
    VERSION_CONFIG=`echo $FLUTTER_VERSIONS | jq -r ".[0].version${INDEX}"`
    list_branch
  done
}

function get_upload_config(){
  UPLOAD_API_CONFIG=("api16" "api18" "api19")
  UPLOAD_API=()

  if [ $# -eq 0 ];then
    UPLOAD_API=("${UPLOAD_API_CONFIG[@]}")
  else
    for i in $*
    do
      if [[ "${UPLOAD_API_CONFIG[@]}" =~ "$i" ]] && [[ ! "${UPLOAD_API[@]}" =~ "$i" ]];then
        UPLOAD_API+=("$i")
      fi
    done;
  fi

  if [ ${#UPLOAD_API[@]} == 0 ];then
    echo "no valid upload api version"
    exit 0
  else
    echo "upload api version: \c"
    for((i=0;i<${#UPLOAD_API[@]};i++)) do
      echo "${UPLOAD_API[i]} \c"
    done;
    echo "."
  fi
}

SCRIPT_DIR=$(cd $(dirname $0);pwd)
HOST_ARCH=""
get_host_arch
get_upload_config $@

BUILD_FLUTTER_VERSION=("3.7" "3.22")
CONFIG_JSON=$SCRIPT_DIR/arch/$HOST_ARCH/build_config.json
FLUTTER_VERSIONS=""
VERSION_CONFIG=""
BRANCH_CONFIG=""
BRANCH_DATA=""

list_flutter_version